package com.jy.adscm.zltx.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.QsUpdatedFileTrain;
import com.jy.adscm.zltx.service.QsUpdatedFileTrainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-10-30
 */
@RestController
@RequestMapping("/adscm/zltx/qsUpdatedFileTrain")
public class QsUpdatedFileTrainController {
    @Autowired
    QsUpdatedFileTrainService qsUpdatedFileTrainService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
      @RequestMapping("/insert")
      public SimpleCoreResponse insert(QsUpdatedFileTrain qsUpdatedFileTrain , HttpServletRequest request)throws AdscmSystemException {
          String tokenId = request.getParameter("tokenId");
          Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
          if (token==null){
              throw  new AdscmSystemException("没找到登录信息");
          }
          qsUpdatedFileTrain.setTrainer(token.getUser().getId().toString());
          boolean result = qsUpdatedFileTrainService.insert(qsUpdatedFileTrain);
          return new SimpleCoreResponse("200","插入成功",result);
      }
    @RequestMapping("/update")
      public  SimpleCoreResponse update(@RequestBody QsUpdatedFileTrain qsUpdatedFileTrain, HttpServletRequest request )throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        if (token==null){
            throw  new AdscmSystemException("没找到登录信息");
        }
          qsUpdatedFileTrain.setTrainer(token.getUser().getId().toString());
          boolean result = qsUpdatedFileTrainService.updateById(qsUpdatedFileTrain);
          return new SimpleCoreResponse("200","修改成功",result);
      }
    @RequestMapping("/delete/{id}")
      public  SimpleCoreResponse delete(@PathVariable Integer id)throws  AdscmSystemException{
          boolean result = qsUpdatedFileTrainService.deleteById(id);
          return  new SimpleCoreResponse("200","删除成功",result);
      }
      @RequestMapping("/selectByFileId/{fileId}")
     public  SimpleCoreResponse selectByFileId(@PathVariable Integer fileId )throws AdscmSystemException{
          List<QsUpdatedFileTrain> qsUpdatedFileTrains = qsUpdatedFileTrainService.selectByFileId(fileId);
          return  new SimpleCoreResponse("200","查询成功",qsUpdatedFileTrains);
     }
}

